package cuishifeng.blog.helper;

import org.apache.commons.lang.StringEscapeUtils;

import com.googlecode.htmlcompressor.compressor.HtmlCompressor;

/**
 * @author cuishifeng <cuishifeng0207@163.com>
 * Created on 2021-02-08
 */
public class HtmlHelper {

    /**
     * html文本压缩
     *
     * @param text html文本
     */
    public static String htmlCompress(String text) throws Exception {
        HtmlCompressor compressor = new HtmlCompressor();
        compressor.setEnabled(true);
        compressor.setCompressCss(true);
        compressor.setRemoveQuotes(true);
        // js 压缩还需处理
        compressor.setYuiJsPreserveAllSemiColons(true);
        compressor.setYuiJsLineBreak(1);
        compressor.setPreserveLineBreaks(false);
        compressor.setRemoveIntertagSpaces(true);
        compressor.setRemoveComments(true);
        compressor.setRemoveMultiSpaces(true);
        return compressor.compress(text);
    }

    /**
     * html标签转义
     *
     * @param html html
     */
    public static String escapeHtml(String html) {
        return StringEscapeUtils.escapeHtml(html);
    }

    /**
     * html标签去转义
     *
     * @param html html
     */
    public static String unescapeHtml(String html) {
        return StringEscapeUtils.unescapeHtml(html);
    }


}
